package casino;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;

public class ScrollBar extends MenuItem {
	private boolean active;
	private Rectangle scroll;
	private int offset;
	private int lTot;
	private boolean scrolling;
	
	public ScrollBar(Menu m,int x,int y,int h, int s){
		super(m,x,y,20,h,"",0);
		scroll = new Rectangle(x,y,20,h);
		active = false;
		lTot = s;
		scrolling = false;
	}
	
	public void update(){
		if(height >= lTot){
			active = false;
		}else{
			active = true;
			if(scrolling) setCurrentScroll(host.casino.relMousePos.y-offset);
			if(scroll.y < y){
				scroll.y = y;
			}else if(scroll.y+scroll.height > y+height){
				scroll.y = y+height-scroll.height;
			}
		}
	}
	
	public void mousePress(MouseEvent e){
		if(scroll.contains(e.getX(),e.getY())){
			scrolling = true;
			offset = e.getY()-scroll.y;
		}
	}
	
	public void mouseRelease(MouseEvent e){
		scrolling = false;
	}
	
	public void setPaneHeight(int h){
		lTot = h;
		scroll.height = (int)(((float)height/lTot)*height);
		if(scroll.height > height){
			scroll.height = height;
		}
	}
	
	private void setCurrentScroll(int s){
		scroll.y = s;
	}
	
	public void setPaneFOV(int f){
		height = f;
		scroll.height = (int)(((float)height/lTot)*height);
		if(scroll.height > height){
			scroll.height = height;
		}
	}
	
	public int offset(){
		return ((scroll.y-y)*lTot)/height;
		//return lCur;
	}
	
	@Override
	protected void draw(Graphics2D g) {
		if(active){
			g.setColor(Color.gray);
			g.fillRect(scroll.x, scroll.y, scroll.width, scroll.height);
			g.setColor(Color.black);
			g.drawRect(scroll.x, scroll.y, scroll.width, scroll.height);
		}
	}

}
